/**
 * 
 */
package com.finalist.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.FlushModeType;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.finalist.dao.InvoiceTitleDao;
import com.finalist.entity.InvoiceTitle;
import com.finalist.entity.InvoiceTitle.InvoiceType;
import com.finalist.entity.InvoiceTitle.SelectStatus;

/**
 * Dao-发票台头
 * @author pengyb
 *
 */
@Repository("invoiceTitleDaoImpl")
public class InvoiceTitleDaoImpl extends BaseDaoImpl<InvoiceTitle, Long> implements InvoiceTitleDao
{

    @Override
    public List<InvoiceTitle> findByUserId(Long memberId)
    {
        if (memberId == null)
        {
            return new ArrayList<InvoiceTitle>();
        }
        String jpql = "select invoiceTitle from InvoiceTitle invoiceTitle where invoiceTitle.memberId = :memberId order by invoiceTitle.modifyDate desc";
        List<InvoiceTitle> invoiceTitleList = entityManager.createQuery(jpql, InvoiceTitle.class).setFlushMode(FlushModeType.COMMIT).setParameter("memberId", memberId).getResultList();
        if (CollectionUtils.isEmpty(invoiceTitleList))
        {
            invoiceTitleList = new ArrayList<InvoiceTitle>();
        }
        return invoiceTitleList;
    }

    @Override
    public void updateSelectStatus(Long memberId)
    {
        String jpql = "update InvoiceTitle invoiceTitle set invoiceTitle.selectStatus=:selectStatus  where invoiceTitle.memberId = :memberId and invoiceTitle.selectStatus=:orgStatus";
        entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("memberId", memberId).setParameter("selectStatus", SelectStatus.notselect).setParameter("orgStatus", SelectStatus.select).executeUpdate();
    }

    @Override
    public void updateSelectStatusById(Long id, SelectStatus selectStatus)
    {
        String jpql = "update InvoiceTitle invoiceTitle set invoiceTitle.selectStatus=:selectStatus  where invoiceTitle.id = :id ";
        entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("id", id).setParameter("selectStatus", selectStatus).executeUpdate();
    }

    @Override
    public InvoiceTitle findByTypeAndTitle(Long memberId, InvoiceType invoiceType, String title)
    {
        String jpql = "select invoiceTitle from InvoiceTitle invoiceTitle where invoiceTitle.memberId = :memberId and invoiceTitle.invoiceType=:invoiceType and invoiceTitle.title=:title";
        List<InvoiceTitle> invoiceTitleList = entityManager.createQuery(jpql, InvoiceTitle.class).setFlushMode(FlushModeType.COMMIT).setParameter("memberId", memberId).setParameter("invoiceType", invoiceType).setParameter("title", title).getResultList();
        InvoiceTitle resultInvoiceTitle = null;
        if (CollectionUtils.isNotEmpty(invoiceTitleList))
        {
            resultInvoiceTitle = invoiceTitleList.get(0);
        }
        return resultInvoiceTitle;
    }
}
